export default function (canvas) {
    if (!canvas) {
        console.warn('Canvas is null, cannot calculate areas')
        return {}
    }
    
    const ctx = canvas.getContext('2d')
    if (!ctx) {
        console.warn('Cannot get canvas context')
        return {}
    }
    
    const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height)
    const data = imageData.data
    const map = new Map()
    
    // 添加调试信息
    console.log('开始计算画布面积，画布尺寸:', canvas.width, 'x', canvas.height)
    
    for (let i = 0; i < data.length; i += 4) {
        const r = data[i]
        const g = data[i + 1]
        const b = data[i + 2]
        const a = data[i + 3] // Alpha channel
        
        // 跳过透明像素（alpha < 128）
        if (a < 128) continue
        
        // 跳过白色像素（默认背景）
        if (r === 255 && g === 255 && b === 255) continue
        
        const color = `rgb(${r},${g},${b})`
        map.set(color, (map.get(color) || 0) + 1)
        
        // 同时记录十六进制格式，用于匹配前端颜色
        const hexColor = `#${r.toString(16).padStart(2, '0')}${g.toString(16).padStart(2, '0')}${b.toString(16).padStart(2, '0')}`
        map.set(hexColor, (map.get(hexColor) || 0) + 1)
    }
    
    const result = Object.fromEntries(map)
    console.log('面积计算结果:', result)
    return result
}
